//
//  EditCustomerInfoController.m
//  FactorySale
//
//  Created by 主事丫环HU on 2020/5/19.
//  Copyright © 2020 主事丫环. All rights reserved.
//

#import "EditCustomerInfoController.h"
#import "UITextView+ZWPlaceHolder.h"
#import "NetLoadAreaPickerViewController.h"
#import "AddAreaModel.h"
@interface EditCustomerInfoController ()
@property (weak, nonatomic) IBOutlet UITextField *nameTextField;
@property (weak, nonatomic) IBOutlet UITextField *phoneTextField;
@property (weak, nonatomic) IBOutlet UITextField *addressTextField;
@property (weak, nonatomic) IBOutlet UITextField *detailAddressTextField;
@property (weak, nonatomic) IBOutlet UITextView *remarksTextView;
@property (nonatomic,strong) NSString *address;
@end

@implementation EditCustomerInfoController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setupUI];
}

- (void)setupUI{
    self.title = @"修改客户信息";
    self.address = self.model.address;
    [self.leftbutton setImage:imgname(@"left_icon") forState:(UIControlStateNormal)];
    self.view.backgroundColor = Main_BackGroundColor;
    self.remarksTextView.zw_placeHolder = @"请输入客户备注信息";
    

    self.nameTextField.text = self.model.name;
    self.phoneTextField.text = self.model.phone;
    NSArray *addressArray = [self.model.address componentsSeparatedByString:@" "];
    if (addressArray.count>0) {
        NSString * addressStr = addressArray[0];
        NSString * detailAddress = addressArray[1];
        self.addressTextField.text = addressStr;
        self.detailAddressTextField.text = detailAddress;
    }
    self.remarksTextView.text = self.model.remarks;
    
    [self.nameTextField addTarget:self action:@selector(nameTextChange) forControlEvents:UIControlEventEditingChanged];
    [self.phoneTextField addTarget:self action:@selector(phoneTextChange) forControlEvents:UIControlEventEditingChanged];
    [self.detailAddressTextField addTarget:self action:@selector(detailAddressTextChange) forControlEvents:UIControlEventEditingChanged];
    self.remarksTextView.delegate = self;
    
}
- (void)textViewDidChange:(UITextView *)textView{
    self.remarksTextView.text = [self disable_emoji:textView.text];
}
- (void)nameTextChange{
    self.nameTextField.text = [self disable_emoji:self.nameTextField.text];
}
- (void)phoneTextChange{
    self.phoneTextField.text = [self disable_emoji:self.phoneTextField.text];
}
- (void)detailAddressTextChange{
    self.detailAddressTextField.text = [self disable_emoji:self.detailAddressTextField.text];
    self.detailAddressTextField.text = [self filterSpaces:self.detailAddressTextField.text];
}
- (IBAction)selectAddressButtonClick:(UIButton *)sender {
    NetLoadAreaPickerViewController * loadAreaVC = [[NetLoadAreaPickerViewController alloc]init];
 
    loadAreaVC.selectedBlock = ^(NetLoadAreaModel *areaModel) {
        KMyLog(@"选中的地址%@",areaModel);
        AddAreaModel *model = [[AddAreaModel alloc]init];
        model.area = [NSString stringWithFormat:@"%@%@%@", areaModel.provinceName, areaModel.cityName, areaModel.districtName];
        model.idstr = areaModel.districtCode;
        model.isdelde = @"0";
       
        self.addressTextField.text = [NSString stringWithFormat:@"%@%@%@",areaModel.provinceName,areaModel.cityName,areaModel.districtName];
        self.address = [NSString stringWithFormat:@"%@%@%@",areaModel.provinceName,areaModel.cityName,areaModel.districtName];
        
    };
    if ([[[UIDevice currentDevice] systemVersion] floatValue]>=8.0) {
        loadAreaVC.modalPresentationStyle=UIModalPresentationOverCurrentContext;
    } else {
        loadAreaVC.modalPresentationStyle=UIModalPresentationCurrentContext;
    }
    [self presentViewController:loadAreaVC animated:NO completion:^{
        
    }];
    return;
}

- (IBAction)confirmButtonClick:(UIButton *)sender {
    if ([self.nameTextField.text isEqualToString:@""]) {
        ShowToastWithText(@"请输入客户姓名");
        return;
    }
    if ([self.phoneTextField.text isEqualToString:@""]) {
        ShowToastWithText(@"请输入联系方式");
        return;
    }
    if ([self.address isEqualToString:@""]) {
        ShowToastWithText(@"请选择收货地址");
        return;
    }
    if ([self.detailAddressTextField.text isEqualToString:@""]) {
        ShowToastWithText(@"请输入详细地址");
        return;
    }
    NSMutableDictionary *param = [NSMutableDictionary dictionary];
    param[@"id"] = self.model.id;
    param[@"name"] = self.nameTextField.text;
    param[@"phone"] = self.phoneTextField.text;
    param[@"address"] = [NSString stringWithFormat:@"%@ %@",self.address,self.detailAddressTextField.text];
    param[@"remarks"] = self.remarksTextView.text;
    
    NSString *json = [HFTools toJSONString:param];
    NSDictionary *jsonParam = @{@"data":json};
    [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    [NetWorkTool POST:updateCsFactoryUser param:jsonParam success:^(id dic) {
        [MBProgressHUD hideHUDForView:self.view animated:YES];
        if (self.editCustomerSuccessfulClick) {
            self.editCustomerSuccessfulClick();
        }
        [self.navigationController popViewControllerAnimated:YES];
    } other:^(id dic) {
        [MBProgressHUD hideHUDForView:self.view animated:YES];
        ShowToastWithText(dic[@"msg"]);
    } fail:^(NSError *error) {
        [MBProgressHUD hideHUDForView:self.view animated:YES];
    } needUser:YES];
}
// 过滤空格
- (NSString *)filterSpaces:(NSString *)text{
    text = [text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
    text = [text stringByReplacingOccurrencesOfString:@" " withString:@""];
    text = [text stringByReplacingOccurrencesOfString:@"\n"withString:@""];
    return text;
}
/**
 *  过滤字符串中的emoji
 */
- (NSString *)disable_emoji:(NSString *)text{
    NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"[^\\u0020-\\u007E\\u00A0-\\u00BE\\u2E80-\\uA4CF\\uF900-\\uFAFF\\uFE30-\\uFE4F\\uFF00-\\uFFEF\\u0080-\\u009F\\u2000-\\u201f\r\n]"options:NSRegularExpressionCaseInsensitive error:nil];
    NSString *modifiedString = [regex stringByReplacingMatchesInString:text
                                                               options:0
                                                                 range:NSMakeRange(0, [text length])
                                                          withTemplate:@""];
    return modifiedString;
}
@end
